package com.hxy.manager.activity.today;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.hxy.manager.R;

/**
 * Created by 傲寒宏志 on 2018/1/14.
 */

public class TodayUpdateActivity extends Activity {
    String id;
    ImageView imageView;
    EditText editText7;
    EditText editText8;
    EditText editText9;
    SQLiteDatabase db;
    String mPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.today_update);

        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回
        editText7=(EditText)findViewById(R.id.editText7);
        editText8=(EditText)findViewById(R.id.editText8);
        editText9=(EditText)findViewById(R.id.editText9);
        imageView=(ImageView)findViewById(R.id.imageView7);


        Intent intent = getIntent();
        id = intent.getStringExtra("id");
    //    Toast.makeText(Today_update.this,id,Toast.LENGTH_SHORT).show();
        db= openOrCreateDatabase("My_DB.db",MODE_PRIVATE,null);

        Cursor cursor=db.rawQuery("select * from TodayPrice where id = "+id,null);
        while (cursor.moveToNext()) {
            mPath = cursor.getString(cursor.getColumnIndex("photo"));
            Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex("photo")));
            imageView.setImageBitmap(bmp);
            editText7.setText(cursor.getString(cursor.getColumnIndex("name")));
            editText8.setText(cursor.getString(cursor.getColumnIndex("price")));
            editText9.setText(cursor.getString(cursor.getColumnIndex("date")));
        }

        Button delete =(Button)findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //先要删除图片文件
                File file = new File(mPath);
                file.delete();

                db.execSQL("delete from TodayPrice where id="+id);
                finish();
            }
        });
        Button save =(Button)findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editText7.getText().toString();
                String price = editText8.getText().toString();
                String date = editText9.getText().toString();

                //取当前年月日时分秒最为编码
                Date d = new Date();
                //格式化为一串数字
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String dateNowStr = sdf.format(d);
              //  db.execSQL("delete from TodayPrice where id="+id);
           //     db.execSQL("update TodayPrice set name='"+name+"' price='"+price+"' date='"+date+"' where id="+id);
                db.execSQL("update TodayPrice set name = '"+name+"' where id = "+id);
                db.execSQL("update TodayPrice set price = '"+price+"' where id = "+id);
                db.execSQL("update TodayPrice set date = '"+date+"' where id = "+id);
                db.execSQL("update TodayPrice set time = '"+dateNowStr+"' where id = "+id);
                finish();
            }
        });



    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
      //  getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item){

        switch(item.getItemId()){
        case android.R.id.home:  //默认添加的，返回上一层
		      finish();  
		      break;  }
        return false;
    }

}
